package com.zang.hash;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;


/**
 * @author ZVerify
 * @since 2022/10/12 21:02
 * @see <a href="https://leetcode.cn/problems/intersection-of-two-arrays/submissions/">...</a>
 **/
public class 两个数组的交集 {

    // 使用set效率比较慢，不过想法比较简单
    public int[] intersection(int[] nums1, int[] nums2) {

        HashSet<Integer> hashSet = new HashSet<>();
        HashSet<Integer> result = new HashSet<>();

        for (int i : nums1) {
            hashSet.add(i);
        }

        for (int i : nums2) {
            if (hashSet.contains(i)){
                result.add(i);
            }
        }
        int[] ints = new int[result.size()];

        int i = 0;

        for (Integer integer : result) {
            ints[i++] = integer;
        }
        return ints;
    }

    // 使用字典
    public int[] intersection1(int[] nums1, int[] nums2) {

        int[] ints = new int[1000];

        for (int i : nums1) {
            if (ints[i]<1){
                ints[i]++;
            }
        }
        ArrayList<Integer> list = new ArrayList<>();
        for (int i : nums2) {
            if (ints[i]!=0  && !list.contains(i)){
                list.add(i);
            }
        }

        int[] ints1 = new int[list.size()];
        int i = 0;
        for (Integer integer : list) {
            ints1[i++] = integer;
        }

        return ints1;
    }
}
